$(function () {
    $("#jqGrid").Grid({
        url: '../modulearchives/list',
        rownumWidth:35,
        multiselect: false,
        colModel: [
            {label: '模具号', name: 'moduleNo', index: 'MODULE_NO', width: 80},
            {label: '模架模次', name: 'useNo', index: 'USE_NO', width: 120},
            {label: '镶块模次', name: 'xiangkuaiUseNo', index: 'XIANGKUAI_USE_NO', width: 80},
            {label: '距上次维护模次', name: 'newlyAccumulateMt', index: 'NEWLY_ACCUMULATE_MT', width: 80},
            {label: '统计模板', name: 'moduleTypeName', index: 'MODULE_TYPE', width: 80, },
            {
                label: '备件寿命', width: 40, align: 'center', sortable: false,
                formatter: function (value, col, row) {
                    // return "<button class='btn btn-outline btn-info' onclick='vm.goModuleDetail(\"" + row.id + "\", \"" + row.moduleCode + "\",\"" + row.moduleTypeName + "\")'><i class='fa fa-file-text-o'></i>&nbsp;模具详情</button>";
                    return "<button title='查看' class='btn btn-outline btn-info' onclick='vm.goModuleDetail(\"" + row.id + "\", \"" + row.moduleCode + "\",\"" + row.moduleTypeName + "\")'><i class='fa fa-file-text-o'></i></button>";
                }
            },
            {label: 'id', name: 'id', key: true, hidden: true},
            {label: '模具编码', name: 'moduleCode', hidden: true},
        ]
    });
    vm.getSelect();
    vm.getTypeTree();

});

var setting = {
    data: {
        simpleData: {
            enable: true,
            idKey: "id",
            pIdKey: "superType",
            rootPId: 0
        },
        key: {
            url: "nourl",
            name: "typeName" //name 与 typeName 绑定
        },
    }
};

var moduleTypeTree;
var setting2 = {
    data: {
        simpleData: {
            enable: true,
            idKey: "id",
            pIdKey: "superType",
            rootPId: 0
        },
        key: {
            url: "nourl",
            name: "typeName" //name 与 typeName 绑定
        },
    },
    callback: {
        onClick: function () {
            var node = typeTree.getSelectedNodes();
            vm.q.moduleType = node[0].id;
            console.log( vm.q.moduleType)
            vm.query()
        }
    }
};

var deptSetting = {
    data: {
        simpleData: {
            enable: true,
            idKey: "deptId",
            pIdKey: "parentId",
            rootPId: -1
        },
        key: {
            url: "nourl"
        }
    },
};

var deptTree;

var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        ruleValidates: {
            days:[
                {required: true, message: '最近天数必填！', trigger: 'blur'}
            ],
            action:[
                { required: true, message: '请选择操作类型', trigger: 'change' }
            ]
        } ,
        q: {
            moduleNo:'', // 模具号
            moduleType:''
        },
        detailModal1: false,//模次统计详情是否显示
        // 维护计划概览参数
        overviewParam: {
            yearly: '',
            action: 'download'
        },
        productModelList: [],
        moduleSeriesList: [],
        treeList: {},
        currentStateArray: enumsToList("MODULE_STATE"),
        moduleTypeList: enumsToListWithNull('MODULE_TYPE'),
        deptList:{},
        detailParam:{
            days:'30',//最近多少天
            moldId:'',//模具id
            action:'onlinePreview'//怎么选择的
        }
    },
    methods: {
        ok () {
            const that = this;
            this.$refs.formValidate.validate((valid) => {
                if (valid) {
                   var url = '../modulearchives/'+that.detailParam.action+'?moldId='+that.detailParam.moldId+'&days='+that.detailParam.days;
                   window.open(url,that.detailParam.action == 'onlinePreview' ? '_blank':'')
                    that.detailModal1 = false;
                    that.$refs.formValidate.resetFields();
                } else {

                }
            })
            // this.detailModal1 = false
        },
        cancel () {
            this.detailModal1 = false
            this.$refs.formValidate.resetFields();
        },
        goFileList: function(){
            var id = getSelectedRow("#jqGrid");
            if (!id) {
                return;
            }
            openWindow({
                type: 2,
                title: '<i class="fa fa-bars"></i> 模具资料',
                content: '../sys/ermsfile.html?moduleType=2&id=' + id
            })
        },
        getSelect: function () {
            Ajax.request({
                url: "../moduleseries/queryAll",
                async: true,
                successCallback: function (res) {
                    vm.productModelList = res.list;
                }
            });
            Ajax.request({
                url: "../moduleseriestype/queryAll",
                async: true,
                successCallback: function (res) {
                    vm.moduleSeriesList = res.list;
                }
            });
            Ajax.request({
                url: "../sys/dept/getCJ",
                async: true,
                successCallback: function (res) {
                    vm.deptList = res.deptList;
                }
            });
        },
        getDeptTree: function () {
            //加载分类树
            Ajax.request({
                url: '../sys/user/treeDeptByType2/DT_ZZCJ',
                async: true,
                successCallback: function (r) {
                    deptTree = $.fn.zTree.init($("#deptTree"), deptSetting, r.list);
                    deptTree.expandAll(true);
                    var node = deptTree.getNodeByParam("deptId", vm.moduleArchives.subShop);
                    if (node) {
                        deptTree.selectNode(node);
                        vm.moduleArchives.subShop = node.deptId;
                    } else {
                        node = deptTree.getNodeByParam("deptId", "0");
                        deptTree.selectNode(node);
                    }
                }
            });
        },
        deptTreeShow: function () {
            openWindow({
                title: "选择车间",
                area: ['300px', '450px'],
                content: jQuery("#deptLayer"),
                btn: ['确定', '取消'],
                btn1: function (index) {
                    var node = deptTree.getSelectedNodes();
                    if (node[0] && node[0].deptType === 'DT_ZZCJ') {
                        // //选择上级菜单
                        vm.moduleArchives.subShop = node[0].deptId;
                        vm.moduleArchives.subShopName = node[0].name;
                        layer.close(index);
                    }
                }
            });
        },
        getModuleTypeTree: function () {
            //加载故障类型树
            Ajax.request({
                url: "../moduletype/select",
                async: true,
                successCallback: function (r) {
                    var tree = r.typeList;
                    moduleTypeTree = $.fn.zTree.init($("#moduleTypeTree"), setting, tree);
                    moduleTypeTree.expandAll(true);
                    var node = moduleTypeTree.getNodeByParam("id", vm.moduleArchives.moduleType);
                    if (node) {
                        moduleTypeTree.selectNode(node);
                        if (node.parentName) {
                            vm.moduleArchives.moduleTypeName = node.parentName;
                        } else {
                            vm.moduleArchives.moduleTypeName = '无';
                        }
                    } else {
                        node = moduleTypeTree.getNodeByParam("id", "0");
                        moduleTypeTree.selectNode(node);
                    }
                }
            });
        },
        typeTree: function () {
            openWindow({
                title: "选择模具类型",
                area: ['300px', '450px'],
                content: jQuery("#moduleTypeLayer"),
                btn: ['确定', '取消'],
                btn1: function (index) {
                    var node = moduleTypeTree.getSelectedNodes();
                    if (node.length != 0) {
                        vm.moduleArchives.moduleType = node[0].id;
                        vm.moduleArchives.moduleTypeName = node[0].typeName;
                    } else {
                        vm.moduleArchives.moduleType = 0;
                        vm.moduleArchives.moduleTypeName = '未选择';
                    }
                    layer.close(index);
                }
            });
        },
        query: function () {
            vm.reload();
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                search: true,
                postData: {
                    'moduleNo': vm.q.moduleNo,
                    'moduleType':vm.q.moduleType
                },
                page: page
            }).trigger("reloadGrid", [{page:1}]);
            vm.handleReset('formValidate');
        },
        reloadSearch: function () {
            vm.q = {
                moduleNo: '', // 模具号
                moduleType:''
            };
            typeTree.cancelSelectedNode();
            vm.reload();
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        },
        /**
         * 查看模具详情
         */
        goModuleDetail: function (id, moduleCode, moduleTypeName) {
            this.detailParam.moldId = id;
            this.detailParam.days = '';
            this.detailParam.action = '';
            this.$refs.formValidate.resetFields();
            vm.detailModal1 = true;
        },
        getTypeTree: function () {
            //加载模具类型树
            Ajax.request({
                url: "../moduletype/select",
                async: true,
                successCallback: function (r) {
                    var tree = r.typeList;
                    typeTree = $.fn.zTree.init($("#typeTree"), setting2, tree);
                    typeTree.expandAll(true);
                }
            });
        },
    }
});
